################################################################################
## Group Identities and Parliamentary Debates: Replication package
## Fiva, Nedregård and Øien (2025)

# Description:

## Code to make Figure A.2: "Number of legislators from each party by policy area"
################################################################################

# Packages

library(data.table)
library(dplyr)
library(ggplot2)
library(stringr)


# Directories (wd is set by master.R)

dir      <-  "../data/2_processed_data"
fig.dir  <-  "../results/figures"

# data

d <- fread(paste(dir, "speeches_session_lemma.csv", sep = "/"))
d[, committee_list   := str_split(committee, ",")]

# data cleaning

d[, policy_committee_list   := str_split(policy_com, "\\|")]
npd.com <- d[, .(policy_com = unlist(policy_committee_list)), by = c("pid_session", "party", "session")]
npd.com[, policy_com := str_trim(policy_com)]

npd.com.plot <- npd.com[, .(n = length(unique(pid_session))), by = c("session", "policy_com", "party")]

## Expand such that we get the count of 0 if not representend in policy com

d.expand <- expand.grid(session = unique(npd.com$session), party = unique(npd.com$party), 
                        policy_com = unique(npd.com$policy_com))

setDT(d.expand)

setkey(npd.com.plot, party, session)

npd.com.plot <- merge(d.expand, npd.com.plot, 
                      by = c("party", "session", "policy_com"), all.x = T)

npd.com.plot[is.na(n), n := 0]

setkey(npd.com.plot, session, policy_com)
com_names <- unique(npd.com.plot$policy_com)

npd.com.plot.total <- npd.com.plot[, .(n = sum(n)), by = c("party", "session")]
npd.com.plot.total[, policy_com := "Total"]

npd.com.plot <- rbindlist(list(npd.com.plot[, .(party, session, policy_com, n)], npd.com.plot.total), use.names = T)
npd.com.plot[, Party := factor(party, levels = c("SV", "A", "Sp", "V", "KrF", "H", "FrP"))]



pdf(paste(fig.dir, "figA2.pdf", sep = "/"), 
    height=40, 
    width=50,
    pointsize = 50)


par(mfrow=c(3,4), mar=c(4,4,2,2), las=1)

for (j in 1:length(com_names)){
  boxplot(n ~ Party, data = npd.com.plot[policy_com == com_names[j]], 
          range=0, 
          main=com_names[j],
          cex.main=1.6,
          ylim=c(0,11),
          xaxt = "n",
          yaxt = "n",
          xlab = "")
  axis(2, seq(0,11,1), las=2, cex.axis=2)
  axis(1, at=1:7, labels=levels(npd.com.plot$Party), cex.axis=1.3, mgp=c(1,1.5,0))
}

boxplot(n ~ Party, data = npd.com.plot[policy_com == "Total"], 
        range=0, 
        main="Total",
        cex.main=1.6,
        ylim=c(0,70),
        xaxt = "n",
        yaxt = "n",
        xlab = "")
axis(2, seq(0,100,10), las=2, cex.axis=2)
axis(1, at=1:7, labels=levels(npd.com.plot$Party), cex.axis=1.3, mgp=c(1,1.5,0))

dev.off()

